package com.wanxi.dao.Impl;

import com.wanxi.dao.ProductTypeDao;
import com.wanxi.model.ProductType;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductTypeDaoImpl implements ProductTypeDao {
    @Override
    public List<ProductType> findAllType() {
        List<ProductType> list=new ArrayList<>();
        String sql="select * from product_type";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                list.add(ProductType.builder()
                        .id(query.getInt("id"))
                        .productTypeName(query.getString("productTypeName"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public ProductType findProductTypeById(ProductType productType) {
        String sql="select * from product_type where id="+productType.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return ProductType.builder()
                        .id(query.getInt("id"))
                        .productTypeName(query.getString("productTypeName"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .build();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updateEnable(ProductType productType) {
        String sql="update product_type set enable="+productType.getEnable()
                +",updateTime=(select now()) where id="+productType.getId();
        return JDBC.update(sql);
    }

    @Override
    public int addProductType(ProductType productType) {
        String sql="insert into product_type(productTypeName,enable,remark,createTime,updateTime) " +
                "values('"+productType.getProductTypeName()
                +"',"+productType.getEnable()
                +",'"+productType.getRemark()
                +"',(select now()),(select now()))";
        return JDBC.update(sql);
    }

    @Override
    public int deleteProductType(ProductType productType) {
        String sql="delete from product_type where id="+productType.getId();
        int i = JDBC.update(sql);
        if(i==1){
            String updateProductTypeIdSql="update product set productTypeID = " +
                    "(select id from product_type limit 0,1)" +
                    ",enable=0,remark='类型被删除',updateTime=(select now()) where productTypeID="+productType.getId();
            JDBC.update(updateProductTypeIdSql);
        }
        return i;
    }

    @Override
    public int updateProductType(ProductType productType) {
        String sql="update product_type set productTypeName='"+productType.getProductTypeName()
                +"',enable="+productType.getEnable()
                +",remark='"+productType.getRemark()
                +"',updateTime=(select now()) where id="+productType.getId();
        return JDBC.update(sql);
    }
}
